<?php
namespace app\index\controller;

use app\common\controller\HomeBase;
use app\common\model\Article as ArticleModel;
use app\common\model\Articlecate as ArticlecateModel;
use think\Cache;
use Captcha\Captcha;
use think\Config;
use think\Db;
use think\Request;
use think\Session;

class About extends HomeBase
{
    protected $site_config;
    protected $category_model;
    protected $article_model;

    public function _initialize()
    {
        parent::_initialize();
        $this->category_model = new ArticlecateModel();
        $this->article_model  = new ArticleModel();
        $category_level_list  = $this->category_model->catetree();
        $this->assign('category_level_list', $category_level_list);
        if (CBOPEN == 2) {
            $this->redirect(url('bbs/index/index'));
        }
        
        $this->site_config = Cache::get('site_config');
    }



    // 单独页面的验证码显示
    public function captcha()
    {
        $m = new Captcha(Config::get('captcha'));

        $img = $m->entry();
        return $img;
    }


    // 网站的首页
    public function index()
    {
        //查询所有的about信息表
        $carousel = Db::name('about')->where('open', 1)->order('time desc')->find();
        $this->assign('carousel', $carousel);

        return yasuo_html($this->fetch());
    }


    // 展示单页的提交页面代码
    public function tianjia()
    {
        return yasuo_html($this->fetch('urladd'));

    }


    // 网站页面单页
    public function html($id='')
    {
        $request = Request::instance();
        $r =  $request->url(true);
        if(!$id){
          $id = is_number(input('id')) ? input('id') : '';
        }
        if (empty($id)) {
            throw new \think\exception\HttpException(404, 'No Page');
        } else {
       $about_shuju = 'about_'.$id;
        if (Cache::has($about_shuju)) {
            $carousel = Cache::get($about_shuju);
        } else {
            $carousel = Db::name('about')->where('id',$id)->order('time desc')->find();
            if(empty($carousel)){
                throw new \think\exception\HttpException(404, 'No Page');
            }
                //根据模板找到所需要的渲染页面
                $carousel['keyword_arr']=explode(',',$carousel['keywords']);
                // 此代码去除url中的http(s)头和/尾部
            Cache::set($about_shuju, $carousel);
        }
            
            
            
           
            $this->assign('t', $carousel);
             $this->assign('r', $r);
            return yasuo_html($this->fetch());
        }
    }



    // 自动提交网站信息到网页数据库中
    public function save()
    {
 
        if ($this->request->isPost()) {
            $data = $this->request->param();
            if(empty($data['yzm'])){
                return "验证码不能是空的!";
            }
           if(!captcha_check($data['yzm'])){
            $flag=0;
           }else{
            $flag=1;
           }
        if($flag!=1){
        return '验证码错误！';
        }
           $data['title'] = remove_xss($data['title']);
           $data['outlink'] = remove_xss($data['outlink']);
           $data['keywords'] = remove_xss($data['keywords']);
           $data['content'] = remove_xss($data['content']);
           $data['tid'] = remove_xss($data['tid']);

            $data['uid']=1;
            // 一定要待审
            $data['open']=0;
            $data['tiaozhuan']=0;
            $data['choice']=0;
            $data['settop']=0;
            $data['view']=mt_rand(0,999);
            $data['updatetime']=time();
            $data['ftitle']="用户自主提交页面，需要重新编辑";
            $data['time']=time();
            $data['coverico']="";
            if($this->article_model->allowField(true)->save($data)){
                return '提交成功';
            }else{
                return "提交失败！";
            }

        }
    }
}
